Change minimizer¶
This example shows how to change the minimization engine for refinement.
Import¶
import easydiffraction as ed
Job¶
Create a job — the main object to store all the information
job = ed.Job()
Model¶
Load a phase from CIF file
job.add_phase_from_file('data/lbco.cif')
print(job.phases)
Collection of 1 phases: ['lbco']
Show phase info in CIF format
phase = job.phases['lbco']
print(phase.cif)
data_lbco _cell_length_a 3.88 _cell_length_b 3.88 _cell_length_c 3.88 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _space_group_name_H-M_ref 'P m -3 m' loop_ _atom_site_label _atom_site_type_symbol _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy _atom_site_adp_type _atom_site_B_iso_or_equiv La La 0.00000000 0.00000000 0.00000000 0.5 Biso 0.49 Ba Ba 0.00000000 0.00000000 0.00000000 0.5 Biso 0.49 Co Co 0.5 0.5 0.5 1.00000000 Biso 0.26 O O 0.00000000 0.5 0.5 1.00000000 Biso 1.4
Display the crystal structure of a given model
job.show_crystal_structure(id='lbco')
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
Experiment¶
Load experimentally measured data from a file in XYE format
job.add_experiment_from_file('data/hrpt.xye')
Display the experimentally measured data
job.show_experiment_chart()
Define a point background
background_points = [(10.0, 170),
(165.0, 170)]
job.set_background(background_points)
Display the experiment chart after setting the background
job.show_experiment_chart()
Analysis¶
Display the analysis chart before setting initial parameter values
job.show_analysis_chart()
Create aliases for the two types of experimental parameters
pattern_params = job.pattern
experiment_params = job.parameters
Change the default value of the wavelength used in the experiment and display the analysis chart again
experiment_params.wavelength = 1.494
job.show_analysis_chart()
Select parameters to be refined
phase.scale.free = True
phase.cell.length_a.free = True
pattern_params.zero_shift.free = True
experiment_params.resolution_u.free = True
experiment_params.resolution_v.free = True
experiment_params.resolution_w.free = True
experiment_params.resolution_y.free = True
Set the initial values of the parameters to be refined and display the analysis chart again
phase.scale = 7
phase.cell.length_a = 3.88
pattern_params.zero_shift = 0.3
experiment_params.resolution_u = 0.1
experiment_params.resolution_v = -0.1
experiment_params.resolution_w = 0.2
experiment_params.resolution_y = 0
job.show_analysis_chart()
Print parameters to be refined (free parameters) before fitting with Lmfit
job.print_free_parameters()
| names | values | errors | units | |
|---|---|---|---|---|
| 1 | length_a | 3.88 | 0.0 | Å |
| 2 | scale | 7.00 | 0.0 | |
| 3 | resolution_u | 0.10 | 0.0 | |
| 4 | resolution_v | -0.10 | 0.0 | |
| 5 | resolution_w | 0.20 | 0.0 | |
| 6 | resolution_y | 0.00 | 0.0 |
Show the current minimization engine
print(job.analysis.current_minimizer)
LMFit_leastsq
Start minimization using the default minimizer (Least Squares method from the Lmfit library)
job.fit()
Fitting successful Duration: 3.60 s Reduced chi: 1.31
Print the refined parameters after fitting with Lmfit
job.print_free_parameters()
| names | values | errors | units | |
|---|---|---|---|---|
| 1 | length_a | 3.890880 | 0.000040 | Å |
| 2 | scale | 9.189503 | 0.032332 | |
| 3 | resolution_u | 0.081718 | 0.003210 | |
| 4 | resolution_v | -0.114989 | 0.006877 | |
| 5 | resolution_w | 0.118157 | 0.003306 | |
| 6 | resolution_y | 0.087131 | 0.001970 | |
| 7 | zero_shift | 0.622782 | 0.001079 | deg |
Display the analysis chart after the fitting with Lmfit
job.show_analysis_chart()
Show list of available minimisation engines
print(job.analysis.available_minimizers)
['LMFit', 'LMFit_leastsq', 'LMFit_powell', 'LMFit_cobyla', 'LMFit_differential_evolution', 'LMFit_scipy_least_squares', 'Bumps', 'Bumps_simplex', 'Bumps_newton', 'Bumps_lm', 'DFO', 'DFO_leastsq']
Change the minimization method to simplex from the Bumps library
job.analysis.current_minimizer = 'Bumps_simplex'
print(job.analysis.current_minimizer)
Bumps_simplex
Reset the free parameters to their initial values (before fitting with Lmfit) and display the analysis chart
phase.scale = 7
phase.cell.length_a = 3.88
pattern_params.zero_shift = 0.3
experiment_params.resolution_u = 0.1
experiment_params.resolution_v = -0.1
experiment_params.resolution_w = 0.2
experiment_params.resolution_y = 0
job.show_analysis_chart()
Print free parameters before the fitting with Bumps
job.print_free_parameters()
| names | values | errors | units | |
|---|---|---|---|---|
| 1 | length_a | 3.88 | 0.000040 | Å |
| 2 | scale | 7.00 | 0.032332 | |
| 3 | resolution_u | 0.10 | 0.003210 | |
| 4 | resolution_v | -0.10 | 0.006877 | |
| 5 | resolution_w | 0.20 | 0.003306 | |
| 6 | resolution_y | 0.00 | 0.001970 | |
| 7 | zero_shift | 0.30 | 0.001079 | deg |
Start minimization with Bumps
job.fit()
Fitting successful Duration: 77.59 s Reduced chi: 1.31
Print free parameters after the fitting with Bumps
job.print_free_parameters()
| names | values | errors | units | |
|---|---|---|---|---|
| 1 | length_a | 3.890880 | 0.000035 | Å |
| 2 | scale | 9.189502 | 0.028245 | |
| 3 | resolution_u | 0.081723 | 0.002804 | |
| 4 | resolution_v | -0.114994 | 0.006008 | |
| 5 | resolution_w | 0.118158 | 0.002888 | |
| 6 | resolution_y | 0.087131 | 0.001721 | |
| 7 | zero_shift | 0.622780 | 0.000943 | deg |
Display the analysis chart after the fitting with Bumps
job.show_analysis_chart()